<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>常用笔记 | 川哥编程路</title>
    <meta name="generator" content="VuePress 1.9.7">
    <link rel="icon" href="/blog/image/favicon.ico">
    <link rel="stylesheet" href="/blog/css/style.css">
    <meta name="description" content="日拱一卒无有尽，功不唐捐终入海">
    
    <link rel="preload" href="/blog/assets/css/0.styles.797dfc43.css" as="style"><link rel="preload" href="/blog/assets/js/app.160d1732.js" as="script"><link rel="preload" href="/blog/assets/js/2.553eb4dc.js" as="script"><link rel="preload" href="/blog/assets/js/23.56de7058.js" as="script"><link rel="preload" href="/blog/assets/js/3.4c6b6694.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.33b61760.js"><link rel="prefetch" href="/blog/assets/js/11.e936ed72.js"><link rel="prefetch" href="/blog/assets/js/12.06a8905b.js"><link rel="prefetch" href="/blog/assets/js/13.43d57349.js"><link rel="prefetch" href="/blog/assets/js/14.9a947fce.js"><link rel="prefetch" href="/blog/assets/js/15.00675827.js"><link rel="prefetch" href="/blog/assets/js/16.4240201a.js"><link rel="prefetch" href="/blog/assets/js/17.4e8e9f2d.js"><link rel="prefetch" href="/blog/assets/js/18.c0804ecf.js"><link rel="prefetch" href="/blog/assets/js/19.e374c696.js"><link rel="prefetch" href="/blog/assets/js/20.dc1f2462.js"><link rel="prefetch" href="/blog/assets/js/21.2550a5e8.js"><link rel="prefetch" href="/blog/assets/js/22.64bef411.js"><link rel="prefetch" href="/blog/assets/js/24.7de4d292.js"><link rel="prefetch" href="/blog/assets/js/25.3483b9cb.js"><link rel="prefetch" href="/blog/assets/js/26.81508f46.js"><link rel="prefetch" href="/blog/assets/js/27.f884547d.js"><link rel="prefetch" href="/blog/assets/js/28.fc8f3edd.js"><link rel="prefetch" href="/blog/assets/js/29.6df73fc7.js"><link rel="prefetch" href="/blog/assets/js/30.92ddc0da.js"><link rel="prefetch" href="/blog/assets/js/31.bc595735.js"><link rel="prefetch" href="/blog/assets/js/32.f6ddf561.js"><link rel="prefetch" href="/blog/assets/js/33.ba353a20.js"><link rel="prefetch" href="/blog/assets/js/34.23530c8e.js"><link rel="prefetch" href="/blog/assets/js/35.98587bae.js"><link rel="prefetch" href="/blog/assets/js/4.e380c258.js"><link rel="prefetch" href="/blog/assets/js/5.9b25da44.js"><link rel="prefetch" href="/blog/assets/js/6.3d19bdbc.js"><link rel="prefetch" href="/blog/assets/js/7.b8795dc7.js"><link rel="prefetch" href="/blog/assets/js/8.9332e1e8.js"><link rel="prefetch" href="/blog/assets/js/9.875f1e10.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.797dfc43.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><!----> <span class="site-name">川哥编程路</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="php" class="dropdown-title"><span class="title">php</span> <span class="arrow down"></span></button> <button type="button" aria-label="php" class="mobile-dropdown-title"><span class="title">php</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/php/base/" class="nav-link">
  通用篇
</a></li><li class="dropdown-item"><!----> <a href="/blog/php/laravel/" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  Laravel
</a></li><li class="dropdown-item"><!----> <a href="/blog/php/skill/" class="nav-link">
  技巧篇
</a></li></ul></div></div><div class="nav-item"><a href="/blog/nginx/" class="nav-link">
  nginx
</a></div><div class="nav-item"><a href="/blog/print/" class="nav-link">
  print
</a></div><div class="nav-item"><a href="/blog/mysql/" class="nav-link">
  mysql
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="web" class="dropdown-title"><span class="title">web</span> <span class="arrow down"></span></button> <button type="button" aria-label="web" class="mobile-dropdown-title"><span class="title">web</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/web/layout/" class="nav-link">
  css布局
</a></li><li class="dropdown-item"><!----> <a href="/blog/web/case/" class="nav-link">
  css实例
</a></li></ul></div></div><div class="nav-item"><a href="/blog/js/" class="nav-link">
  js
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="python" class="dropdown-title"><span class="title">python</span> <span class="arrow down"></span></button> <button type="button" aria-label="python" class="mobile-dropdown-title"><span class="title">python</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/python/base/" class="nav-link">
  python基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/python/machine/" class="nav-link">
  机器学习
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Golang" class="dropdown-title"><span class="title">Golang</span> <span class="arrow down"></span></button> <button type="button" aria-label="Golang" class="mobile-dropdown-title"><span class="title">Golang</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/go/env/" class="nav-link">
  前言和环境
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/grammar/" class="nav-link">
  语法基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/base/" class="nav-link">
  函数和数组(切片)
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/map/" class="nav-link">
  Map,指针,值传递,引用传递
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/struct/" class="nav-link">
  结构体
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/gin/" class="nav-link">
  Gin
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/concurrent/" class="nav-link">
  go并发
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/standard/" class="nav-link">
  常用标准库
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/skills/" class="nav-link">
  技巧篇
</a></li></ul></div></div><div class="nav-item"><a href="/blog/docker/" class="nav-link">
  docker
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="vue" class="dropdown-title"><span class="title">vue</span> <span class="arrow down"></span></button> <button type="button" aria-label="vue" class="mobile-dropdown-title"><span class="title">vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/vue/base/" class="nav-link">
  vue基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/assembly/" class="nav-link">
  组件化编程
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/problem/" class="nav-link">
  problem
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/uniApp/" class="nav-link">
  uniApp
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tool/" class="nav-link">
  tool
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="php" class="dropdown-title"><span class="title">php</span> <span class="arrow down"></span></button> <button type="button" aria-label="php" class="mobile-dropdown-title"><span class="title">php</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/php/base/" class="nav-link">
  通用篇
</a></li><li class="dropdown-item"><!----> <a href="/blog/php/laravel/" aria-current="page" class="nav-link router-link-exact-active router-link-active">
  Laravel
</a></li><li class="dropdown-item"><!----> <a href="/blog/php/skill/" class="nav-link">
  技巧篇
</a></li></ul></div></div><div class="nav-item"><a href="/blog/nginx/" class="nav-link">
  nginx
</a></div><div class="nav-item"><a href="/blog/print/" class="nav-link">
  print
</a></div><div class="nav-item"><a href="/blog/mysql/" class="nav-link">
  mysql
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="web" class="dropdown-title"><span class="title">web</span> <span class="arrow down"></span></button> <button type="button" aria-label="web" class="mobile-dropdown-title"><span class="title">web</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/web/layout/" class="nav-link">
  css布局
</a></li><li class="dropdown-item"><!----> <a href="/blog/web/case/" class="nav-link">
  css实例
</a></li></ul></div></div><div class="nav-item"><a href="/blog/js/" class="nav-link">
  js
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="python" class="dropdown-title"><span class="title">python</span> <span class="arrow down"></span></button> <button type="button" aria-label="python" class="mobile-dropdown-title"><span class="title">python</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/python/base/" class="nav-link">
  python基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/python/machine/" class="nav-link">
  机器学习
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Golang" class="dropdown-title"><span class="title">Golang</span> <span class="arrow down"></span></button> <button type="button" aria-label="Golang" class="mobile-dropdown-title"><span class="title">Golang</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/go/env/" class="nav-link">
  前言和环境
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/grammar/" class="nav-link">
  语法基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/base/" class="nav-link">
  函数和数组(切片)
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/map/" class="nav-link">
  Map,指针,值传递,引用传递
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/struct/" class="nav-link">
  结构体
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/gin/" class="nav-link">
  Gin
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/concurrent/" class="nav-link">
  go并发
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/standard/" class="nav-link">
  常用标准库
</a></li><li class="dropdown-item"><!----> <a href="/blog/go/skills/" class="nav-link">
  技巧篇
</a></li></ul></div></div><div class="nav-item"><a href="/blog/docker/" class="nav-link">
  docker
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="vue" class="dropdown-title"><span class="title">vue</span> <span class="arrow down"></span></button> <button type="button" aria-label="vue" class="mobile-dropdown-title"><span class="title">vue</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/vue/base/" class="nav-link">
  vue基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/assembly/" class="nav-link">
  组件化编程
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/problem/" class="nav-link">
  problem
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/uniApp/" class="nav-link">
  uniApp
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tool/" class="nav-link">
  tool
</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span></span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/php/laravel/" aria-current="page" class="active sidebar-link">常用笔记</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/php/laravel/#laravel部署-文件夹权限问题" class="sidebar-link">Laravel部署,文件夹权限问题</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/php/laravel/#方法一" class="sidebar-link">方法一</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#方法二" class="sidebar-link">方法二</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#事务" class="sidebar-link">事务</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#excel导入数据" class="sidebar-link">Excel导入数据</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#图片处理" class="sidebar-link">图片处理</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#日期值比较" class="sidebar-link">日期值比较</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#where系列" class="sidebar-link">where系列</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/php/laravel/#wherein" class="sidebar-link">whereIn</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#wherecolumn" class="sidebar-link">whereColumn</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#闭包分组or条件" class="sidebar-link">闭包分组or条件</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#when条件语句" class="sidebar-link">when条件语句</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#自增自减" class="sidebar-link">自增自减</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#调试" class="sidebar-link">调试</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#分页" class="sidebar-link">分页</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#更新json字段" class="sidebar-link">更新json字段</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#分组求和排序" class="sidebar-link">分组求和排序</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#距离搜索" class="sidebar-link">距离搜索</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#常用设置" class="sidebar-link">常用设置</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/php/laravel/#队列设置超时" class="sidebar-link">队列设置超时</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#接收参数空字符串被转换为null" class="sidebar-link">接收参数空字符串被转换为null</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#以www用户设置定时任务" class="sidebar-link">以www用户设置定时任务</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#伪静态规则" class="sidebar-link">伪静态规则</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#自定义函数" class="sidebar-link">自定义函数</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#常见bug" class="sidebar-link">常见bug</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/php/laravel/#报错too-many-attempts" class="sidebar-link">报错Too Many Attempts</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#artisan命令系列" class="sidebar-link">artisan命令系列</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/php/laravel/#创建laravel项目" class="sidebar-link">创建Laravel项目</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#路由缓存" class="sidebar-link">路由缓存</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#数据库迁移" class="sidebar-link">数据库迁移</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#定义中间件" class="sidebar-link">定义中间件</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#定义计划任务" class="sidebar-link">定义计划任务</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#orm操作" class="sidebar-link">ORM操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/php/laravel/#模型内部关联" class="sidebar-link">模型内部关联</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#orm中with的写法" class="sidebar-link">ORM中with的写法</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#访问器" class="sidebar-link">访问器</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#修改器" class="sidebar-link">修改器</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#with查找出来的对象访问问题" class="sidebar-link">with查找出来的对象访问问题</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#join关联查询" class="sidebar-link">join关联查询</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#orm关联查询代码" class="sidebar-link">orm关联查询代码</a></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#参考with的访问和修改问题" class="sidebar-link">参考with的访问和修改问题</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/php/laravel/#常用参考资料" class="sidebar-link">常用参考资料</a></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="常用笔记"><a href="#常用笔记" class="header-anchor">#</a> 常用笔记</h1> <p>愉悦要懂得分享 , 才能加倍愉悦 。</p> <p><a href="https://learnku.com/docs/laravel/8.5/routing/10368" target="_blank" rel="noopener noreferrer">参考文档<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h2 id="laravel部署-文件夹权限问题"><a href="#laravel部署-文件夹权限问题" class="header-anchor">#</a> Laravel部署,文件夹权限问题</h2> <div class="custom-block tip"><p class="custom-block-title">Laravel部署之后, 哪些文件夹需要写权限</p> <p>storage 目录包含编译后的 Blade 模板、session 会话生成的文件、缓存文件以及框架生成的其他文件。这个目录被细分成 app 、 framework 和 logs 三个子目录：</p> <ol><li>app 目录可以用来存储应用生成的任何文件；</li> <li>framework 目录用来存储框架生成的文件和缓存；</li> <li>logs 目录包含应用的日志文件。</li></ol></div> <div class="language-text line-numbers-mode"><pre class="language-text"><code>如果 storage 目录的权限不正确，可能导致应用运行出现异常，例如出现：
The stream or file &quot;/var/www/storage/logs/laravel.log&quot; could 
not be opened: failed to open stream: Permission denied
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="方法一"><a href="#方法一" class="header-anchor">#</a> 方法一</h3> <ol><li>更改<code>storage</code>文件夹权限</li></ol> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">sudo</span> <span class="token function">chmod</span> -R 0755 storage
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ol start="2"><li>更改 <code>storage</code> 文件夹拥有者与 <code>php-fpm.conf</code> 运行用户一致，如 <code>php-fpm.conf</code> 配置为:</li></ol> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code>user <span class="token operator">=</span> www
group <span class="token operator">=</span> www
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>则命令为:</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">sudo</span> <span class="token function">chown</span> -R www:www storage
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="方法二"><a href="#方法二" class="header-anchor">#</a> 方法二</h3> <p>更改 <code>storage</code> 文件夹权限为<code>0777</code></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">sudo</span> <span class="token function">chmod</span> -R 0777 storage
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="custom-block danger"><p class="custom-block-title">注意</p> <p>生产服务器上应该遵循「最小权限原则」，推荐使用「方法一」</p></div> <h2 id="事务"><a href="#事务" class="header-anchor">#</a> 事务</h2> <blockquote><p>laravel示例</p></blockquote> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token comment"># use Illuminate\Support\Facades\DB;</span>
<span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">beginTransaction</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">try</span>
<span class="token punctuation">{</span>
	<span class="token comment"># 多个操作数据库业务语句</span>
	<span class="token comment">#  sql</span>
	<span class="token comment">#  sql</span>
	<span class="token comment">#  sql</span>
	<span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">exit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'SUCCESS'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>Exception</span> <span class="token variable">$e</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">rollBack</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">dump</span><span class="token punctuation">(</span><span class="token variable">$e</span><span class="token operator">-&gt;</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">exit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FALSE'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="excel导入数据"><a href="#excel导入数据" class="header-anchor">#</a> Excel导入数据</h2> <p><font color="blue"><strong>安装</strong></font></p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token function">composer</span> require phpoffice/phpexcel
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><font color="blue"><strong>demo</strong></font></p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token comment"># file到导入excel的keyName</span>
<span class="token variable">$upload_file</span> <span class="token operator">=</span> <span class="token variable">$_FILES</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'file'</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'tmp_name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$reader</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Xls</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$spreadsheet</span> <span class="token operator">=</span> <span class="token variable">$reader</span><span class="token operator">-&gt;</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token variable">$upload_file</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$rows</span> <span class="token operator">=</span> <span class="token variable">$spreadsheet</span><span class="token operator">-&gt;</span><span class="token function">getActiveSheet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="图片处理"><a href="#图片处理" class="header-anchor">#</a> 图片处理</h2> <p><a href="https://learnku.com/articles/38826" target="_blank" rel="noopener noreferrer">图片处理参开<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token function">ini_set</span> <span class="token punctuation">(</span><span class="token string single-quoted-string">'memory_limit'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'1024M'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 修改指定图片的大小</span>
<span class="token comment">// $img = \Intervention\Image\Facades\Image::make(storage_path('app/advert/background.png')); # 以这个背景图为新图像实例</span>
<span class="token variable">$img</span> <span class="token operator">=</span>  <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Intervention<span class="token punctuation">\</span>Image<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Image</span><span class="token operator">::</span><span class="token function">canvas</span><span class="token punctuation">(</span><span class="token number">6969</span><span class="token punctuation">,</span> <span class="token number">9331</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'#FFFFFF'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"># 创建常6969px, 宽9331,颜色为#FFFFFF的新图像实例</span>
<span class="token variable">$img</span><span class="token operator">-&gt;</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app/advert/adimg1.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'top'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment"># 插入一张图片在新图像上面</span>
<span class="token variable">$img</span><span class="token operator">-&gt;</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app/advert/bottom.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bottom'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment"># 插入一张图片在新图像上面</span>
<span class="token variable">$img</span><span class="token operator">-&gt;</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app/advert/38fb1615f404ccc2078c71ab2717dd3.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bottom-right'</span><span class="token punctuation">,</span> <span class="token number">742</span><span class="token punctuation">,</span> <span class="token number">633</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment"># 插入一张图片在新图像上面</span>
<span class="token variable">$img</span><span class="token operator">-&gt;</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app/advert/new_image.png'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"># 新图像保存位置</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="日期值比较"><a href="#日期值比较" class="header-anchor">#</a> 日期值比较</h2> <p><font color="blue"><strong><strong>whereDate / whereMonth / whereDay / whereYear / whereTime</strong></strong></font></p> <p><code>created</code> 字段类型为<code>timestamp</code></p> <ul><li><code>whereDate</code> 方法是用来比较字段的值与给定的日期值是否相等 （年 - 月 - 日）</li> <li><code>whereMonth</code> 方法是用来比较字段的值与给定的月份是否相等（月)</li> <li><code>whereDay</code> 方法是用来比较字段的值与一个月中给定的日期是否相等 （日）</li> <li><code>whereYear</code> 方法是用来比较字段的值与给定的年份是否相等（年）</li> <li><code>whereTime</code> 方法是用来比较字段的值与给定的时间是否相等（时：分: 秒）</li></ul> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token operator">+</span> <span class="token function">whereDate</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'&gt;'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'2016-12-31'</span><span class="token punctuation">)</span>
<span class="token operator">+</span> <span class="token function">whereMonth</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'12'</span><span class="token punctuation">)</span>   
<span class="token operator">+</span> <span class="token function">whereDay</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'31'</span><span class="token punctuation">)</span>
<span class="token operator">+</span> <span class="token function">whereYear</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'2016'</span><span class="token punctuation">)</span>
<span class="token operator">+</span> <span class="token function">whereTime</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'11:20:45'</span><span class="token punctuation">)</span>    
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="where系列"><a href="#where系列" class="header-anchor">#</a> where系列</h2> <h3 id="wherein"><a href="#wherein" class="header-anchor">#</a> whereIn</h3> <p>用来验证一个字段的值是否在给定的数组中</p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token function">whereIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="wherecolumn"><a href="#wherecolumn" class="header-anchor">#</a> whereColumn</h3> <p>用来比较两个给定的字段的值是否相等</p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token function">whereColumn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'first_name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last_name'</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="闭包分组or条件"><a href="#闭包分组or条件" class="header-anchor">#</a> 闭包分组or条件</h3> <p><font color="blue"><strong>闭包和when里面return问题:</strong></font>  都可以不写return, 他们都是对象传递, 可以匿名函数内部改变对象, 当你不return回匿名函数结果的时候, 默认是返回当前的query对象, 当你return回匿名函数的时候, 结果为真, 则返回匿名函数结果(你在匿名函数里拼接多个where, 其实where方法还是返回的$this), 为假则还是返回当前的query对象 。</p> <p>如果您需要在括号内对 <code>or</code> 条件进行分组，那么可以传递一个闭包作为 <code>orWhere</code> 方法的第一个参数</p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
            <span class="token operator">-&gt;</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'&gt;'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>
            <span class="token operator">-&gt;</span><span class="token function">orWhere</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token variable">$query</span><span class="token operator">-&gt;</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Abigail'</span><span class="token punctuation">)</span>
                      <span class="token operator">-&gt;</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'&gt;'</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span>
            <span class="token operator">-&gt;</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><font color="blue"><strong>等同于:</strong></font></p> <div class="language-php line-numbers-mode"><pre class="language-php"><code>select <span class="token operator">*</span> from users where votes <span class="token operator">&gt;</span> <span class="token number">100</span> <span class="token keyword">or</span> <span class="token punctuation">(</span>name <span class="token operator">=</span> <span class="token string single-quoted-string">'Abigail'</span> <span class="token keyword">and</span> votes <span class="token operator">&gt;</span> <span class="token number">50</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="when条件语句"><a href="#when条件语句" class="header-anchor">#</a> when条件语句</h3> <p>有时候你可能想要子句只适用于某个情况为真时才执行查询。</p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token variable">$role</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-&gt;</span><span class="token function">input</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'role'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
                <span class="token operator">-&gt;</span><span class="token function">when</span><span class="token punctuation">(</span><span class="token variable">$role</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">,</span> <span class="token variable">$role</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token variable">$query</span><span class="token operator">-&gt;</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'role_id'</span><span class="token punctuation">,</span> <span class="token variable">$role</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span><span class="token punctuation">)</span>
                <span class="token operator">-&gt;</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="自增自减"><a href="#自增自减" class="header-anchor">#</a> 自增自减</h2> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">increment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment"># 自增1</span>

<span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">increment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"># 自增5</span>

<span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">decrement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"># 自减1</span>

<span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">decrement</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'votes'</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"># 自减5</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><font color="blue"><strong>自增自减的同时更新数据</strong></font></p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">increment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'money'</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'frozenMoney'</span><span class="token operator">=&gt;</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'status'</span><span class="token operator">=&gt;</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="调试"><a href="#调试" class="header-anchor">#</a> 调试</h2> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token comment"># use Illuminate\Support\Facades\DB;</span>
<span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">connection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">enableQueryLog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">increment</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'money'</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'frozenMoney'</span><span class="token operator">=&gt;</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'status'</span><span class="token operator">=&gt;</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">getQueryLog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h2 id="分页"><a href="#分页" class="header-anchor">#</a> 分页</h2> <p><font color="blue"><strong>paginate</strong></font></p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token property">request</span><span class="token operator">-&gt;</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token operator">-&gt;</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token keyword">empty</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'size'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant boolean">true</span> <span class="token operator">?</span> <span class="token number">10</span> <span class="token punctuation">:</span> <span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'size'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><font color="blue"><strong>paginate进一步处理</strong></font></p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token comment">/**
 * 分页查找数据
 * @param $data object
 * @param $size int 每页条数
 * @return $data array 数组
 */</span>
<span class="token keyword">function</span> <span class="token function-definition function">page</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span>
<span class="token punctuation">{</span>
    <span class="token comment">// 通过分页器实例方法获取附加的分页信息</span>
    <span class="token variable">$list</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'page'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword type-casting">int</span><span class="token punctuation">)</span><span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">currentPage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$list</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'size'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword type-casting">int</span><span class="token punctuation">)</span><span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">perPage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$list</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'totalSize'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword type-casting">int</span><span class="token punctuation">)</span><span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">total</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$list</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'totalPage'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword type-casting">int</span><span class="token punctuation">)</span><span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">?</span> <span class="token number">0</span> <span class="token punctuation">:</span> <span class="token function">ceil</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">total</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$list</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'list'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">items</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token variable">$list</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h2 id="更新json字段"><a href="#更新json字段" class="header-anchor">#</a> 更新json字段</h2> <p>更新 JSON 字段时，你可以使用 <code>-&gt;</code> 语法访问 JSON 对象中相应的值。注意，此操作只能支持 MySQL 5.7+ 和 PostgreSQL 9.5+ ：</p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">table</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'options-&gt;enabled'</span> <span class="token operator">=&gt;</span> <span class="token constant boolean">true</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h2 id="分组求和排序"><a href="#分组求和排序" class="header-anchor">#</a> 分组求和排序</h2> <p>关闭laravel里面的mysql严格模式, 严格模式下, select的字段要保持和groupBy的字段一致; <code>config/datavase.php</code>下<code>strict</code>的值改为<code>false</code></p> <div class="language-php line-numbers-mode"><pre class="language-php"><code> <span class="token variable">$obj</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'superior:id,nickName,avatarUrl,sex,authStatus,memberLevel'</span><span class="token punctuation">)</span>
     <span class="token operator">-&gt;</span><span class="token function">select</span><span class="token punctuation">(</span>
                <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span>
                <span class="token string single-quoted-string">'p_id'</span><span class="token punctuation">,</span>
                <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">raw</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'count(id) as totalNum '</span><span class="token punctuation">)</span>
            <span class="token punctuation">)</span>
     <span class="token comment"># p_id 分组条件</span>
     <span class="token operator">-&gt;</span><span class="token function">groupBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'p_id'</span><span class="token punctuation">)</span>
     <span class="token comment"># 条件判断</span>
     <span class="token operator">-&gt;</span><span class="token function">havingRaw</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'p_id &gt; ? and count(id) &gt; ?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
     <span class="token comment"># 排序</span>
     <span class="token operator">-&gt;</span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">raw</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'count(id)'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'DESC'</span><span class="token punctuation">)</span>
     <span class="token operator">-&gt;</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>
     <span class="token operator">-&gt;</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="距离搜索"><a href="#距离搜索" class="header-anchor">#</a> 距离搜索</h2> <p>存经度, 纬度, 返回给前端使用, 存point空间索引字段, 作为后端计算距离使用, 计算出来的距离单位是<code>km</code></p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token variable">$lng</span> <span class="token operator">=</span> <span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'lng'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>      
<span class="token variable">$lat</span> <span class="token operator">=</span> <span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'lat'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$where</span> <span class="token operator">=</span> <span class="token string double-quoted-string">&quot;1=1&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$range</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment"># $range 范围, 最近多少km以内</span>
    <span class="token variable">$where</span> <span class="token operator">=</span> <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">raw</span><span class="token punctuation">(</span><span class="token string double-quoted-string">&quot;ROUND(st_distance_sphere( point, point ( <span class="token interpolation"><span class="token variable">$lng</span></span>, <span class="token interpolation"><span class="token variable">$lat</span></span> ) ) /1000,2)&quot;</span><span class="token punctuation">)</span> <span class="token operator">.</span> <span class="token string double-quoted-string">&quot; &lt; &quot;</span> <span class="token operator">.</span> <span class="token variable">$range</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token variable">$obj</span> <span class="token operator">=</span> <span class="token class-name static-context">AdvertPoint</span><span class="token operator">::</span><span class="token function">whereRaw</span><span class="token punctuation">(</span><span class="token variable">$where</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$storey</span> <span class="token operator">=</span> <span class="token variable">$obj</span><span class="token operator">-&gt;</span><span class="token function">sum</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'storey'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"># 楼宇总数</span>
<span class="token variable">$unit</span> <span class="token operator">=</span> <span class="token variable">$obj</span><span class="token operator">-&gt;</span><span class="token function">sum</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'unit'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"># 单元总数</span>
<span class="token variable">$elevator</span> <span class="token operator">=</span> <span class="token variable">$obj</span><span class="token operator">-&gt;</span><span class="token function">sum</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'elevator'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment"># 电梯总数</span>
<span class="token variable">$pointObj</span> <span class="token operator">=</span> <span class="token variable">$obj</span>
    <span class="token operator">-&gt;</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
        <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'area'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'address'</span><span class="token punctuation">,</span>       
        <span class="token string single-quoted-string">'lng'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'lat'</span><span class="token punctuation">,</span>
        <span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">raw</span><span class="token punctuation">(</span><span class="token string double-quoted-string">&quot;ROUND(st_distance_sphere( point, point ( <span class="token interpolation"><span class="token variable">$lng</span></span>, <span class="token interpolation"><span class="token variable">$lat</span></span> ) ) /1000,2) AS 'distance'&quot;</span><span class="token punctuation">)</span> <span class="token comment"># 单位km</span>
    <span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'distance'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'ASC'</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token keyword">empty</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'size'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant boolean">true</span> <span class="token operator">?</span> <span class="token number">10</span> <span class="token punctuation">:</span> <span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'size'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token function">page</span><span class="token punctuation">(</span><span class="token variable">$pointObj</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function-definition function">page</span><span class="token punctuation">(</span><span class="token keyword type-hint">object</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span>
<span class="token punctuation">{</span>
    <span class="token comment">// 通过分页器实例方法获取附加的分页信息</span>
    <span class="token variable">$list</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'page'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword type-casting">int</span><span class="token punctuation">)</span><span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">currentPage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$list</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'size'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword type-casting">int</span><span class="token punctuation">)</span><span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">perPage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$list</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'totalSize'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword type-casting">int</span><span class="token punctuation">)</span><span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">total</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$list</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'totalPage'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword type-casting">int</span><span class="token punctuation">)</span><span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">?</span> <span class="token number">0</span> <span class="token punctuation">:</span> <span class="token function">ceil</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">total</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token variable">$list</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'list'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">items</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token variable">$list</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h2 id="常用设置"><a href="#常用设置" class="header-anchor">#</a> 常用设置</h2> <h3 id="队列设置超时"><a href="#队列设置超时" class="header-anchor">#</a> 队列设置超时</h3> <p>队列一般需要用<font color="blue"><strong>Supervisor</strong></font>守护</p> <div class="language-shell line-numbers-mode"><pre class="language-shell"><code><span class="token comment"># 启动命令</span>
/usr/bin/php /www/wwwroot/xindan.api.shuxiaoniu.com/artisan queue:work --queue<span class="token operator">=</span>xindan --timeout<span class="token operator">=</span><span class="token number">0</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="接收参数空字符串被转换为null"><a href="#接收参数空字符串被转换为null" class="header-anchor">#</a> 接收参数空字符串被转换为null</h3> <p>在<code>app\Http\Kernel.php</code>文件夹中，注释全局中间件：</p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Foundation<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Kernel</span> <span class="token keyword">as</span> HttpKernel<span class="token punctuation">;</span>
<span class="token keyword">class</span> <span class="token class-name-definition class-name">Kernel</span> <span class="token keyword">extends</span> <span class="token class-name">HttpKernel</span>
<span class="token punctuation">{</span>   
    <span class="token keyword">protected</span> <span class="token variable">$middleware</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
         <span class="token operator">...</span>
        <span class="token comment">// 下面这个中间件表示: 提交空字符串转为null</span>
<span class="token comment">//        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,</span>
    <span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token operator">...</span>    
<span class="token punctuation">}</span>
</span></code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="以www用户设置定时任务"><a href="#以www用户设置定时任务" class="header-anchor">#</a> 以www用户设置定时任务</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">crontab</span> -u www -e
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="伪静态规则"><a href="#伪静态规则" class="header-anchor">#</a> 伪静态规则</h3> <div class="language-conf line-numbers-mode"><pre class="language-text"><code>location / {
    try_files $uri $uri/ /index.php?$query_string;
 }
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="自定义函数"><a href="#自定义函数" class="header-anchor">#</a> 自定义函数</h3> <ul><li>创建文件 <code>app/helpers.php</code><div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token comment">// 示例函数</span>
<span class="token keyword">function</span> <span class="token function-definition function">foo</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token string double-quoted-string">&quot;foo&quot;</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span></code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li>修改项目<code>composer.json</code>
在项目 <code>composer.json</code>中 <code>autoload</code> 部分里的 <code>files</code> 字段加入该文件即可：<div class="language-conf line-numbers-mode"><pre class="language-text"><code>{
    ...

    &quot;autoload&quot;: {
        &quot;files&quot;: [
            &quot;app/helpers.php&quot;
        ]
    }
    ...
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div></li> <li>最后一步: 运行下面命令<div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">composer</span> dump-autoload
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div>OK，然后你就可以在任何地方用到 <code>app/helpers.php</code> 中的函数了。</li></ul> <h2 id="常见bug"><a href="#常见bug" class="header-anchor">#</a> 常见bug</h2> <h3 id="报错too-many-attempts"><a href="#报错too-many-attempts" class="header-anchor">#</a> 报错<code>Too Many Attempts</code></h3> <p>这是因为<code>laravel</code>从5.2开始，增加了一个Throttle的中间件。在<code>app/Http/Kernel.php</code>文件，你就会发现，<code>api</code>路由是默认使用了这个中间件的。
这个中间件的作用是限制同一个<code>Ip</code>访问同一个<code>Api</code>的访问次数，模式是1分钟内只能访问60次，超过60次，则会返回Too Many Attempts 429状态。需要等待1分钟后才可以访问;
修改:</p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token comment"># app/Http/Kernel.php 先直接注销调这行, 本质原因是代理的原因, 导致所有用户访问都是一个ip</span>
<span class="token string single-quoted-string">'api'</span> <span class="token operator">=&gt;</span> <span class="token punctuation">[</span>
    <span class="token comment">// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,</span>
    <span class="token comment"># 限制同一个Ip访问同一个Api的访问次</span>
    <span class="token comment">#'throttle:api',</span>
    <span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Routing<span class="token punctuation">\</span>Middleware<span class="token punctuation">\</span>SubstituteBindings</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h2 id="artisan命令系列"><a href="#artisan命令系列" class="header-anchor">#</a> artisan命令系列</h2> <h3 id="创建laravel项目"><a href="#创建laravel项目" class="header-anchor">#</a> 创建Laravel项目</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code><span class="token function">composer</span> create-project laravel/laravel poster
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><font style="color:blue;"><strong>注意</strong></font></p> <ul><li><code>poster</code>是项目文件夹名称, 可以自定义</li> <li>window系统用<code>cmd</code>小黑框安装执行, 即root执行</li> <li>不要忘了经常执行 <code>composer selfupdate</code> 以保持 Composer 一直是最新版本哦！</li></ul> <h3 id="路由缓存"><a href="#路由缓存" class="header-anchor">#</a> 路由缓存</h3> <p><font style="color:blue;"><strong>生成路由缓存</strong></font>
如果您的应用程序只使用了基于控制器的路由，那么您应该利用 Laravel 的路由缓存。路由缓存会大大减少注册所有路由所需的时间。在某些情况下，路由注册的速度甚至能快上 100 倍。要生成路由缓存，只需执行 artisan 命令</p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>php artisan route:cache
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>运行此命令后，将在每个请求上加载缓存的路由文件。记住，如果添加了任何新的路由，则需要重新生成新的路由缓存。因此，您应该在项目部署的时候运行 <code>route:cache</code> 命令。
<font style="color:blue;"><strong>清除路由缓存</strong></font></p> <div class="language-php line-numbers-mode"><pre class="language-php"><code>php artisan route<span class="token punctuation">:</span>clear
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="数据库迁移"><a href="#数据库迁移" class="header-anchor">#</a> 数据库迁移</h3> <p><font style="color:blue;"><strong>生成迁移</strong></font></p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>php artisan make:migration create_users_table
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><font style="color:blue;"><strong>执行迁移</strong></font></p> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>php artisan migrate
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="定义中间件"><a href="#定义中间件" class="header-anchor">#</a> 定义中间件</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>php artisan make:middleware xxx
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="定义计划任务"><a href="#定义计划任务" class="header-anchor">#</a> 定义计划任务</h3> <div class="language-sh line-numbers-mode"><pre class="language-sh"><code>php artisan make:command xxx
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="orm操作"><a href="#orm操作" class="header-anchor">#</a> ORM操作</h2> <h3 id="模型内部关联"><a href="#模型内部关联" class="header-anchor">#</a> 模型内部关联</h3> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token comment"># Model</span>
<span class="token keyword">protected</span> <span class="token variable">$fillable</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
        <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'p_id'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'level'</span><span class="token punctuation">,</span>
        <span class="token string single-quoted-string">'url'</span>
    <span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">children</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">HasMany</span>
<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token function">hasMany</span><span class="token punctuation">(</span><span class="token class-name static-context">Address</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'p_id'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment"># 控制器, 查询出一级地址和所有二级地址, 并形成上下级</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">address</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">JsonResponse</span>
<span class="token punctuation">{</span>
    <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token class-name static-context">Address</span><span class="token operator">::</span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
        <span class="token string single-quoted-string">'children:id,name,p_id'</span>
    <span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token operator">-&gt;</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token operator">-&gt;</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'level'</span> <span class="token operator">=&gt;</span> <span class="token class-name static-context">Address</span><span class="token operator">::</span><span class="token constant">LEVEL</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
        <span class="token operator">-&gt;</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h3 id="orm中with的写法"><a href="#orm中with的写法" class="header-anchor">#</a> ORM中with的写法</h3> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token comment"># 第一种</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">address</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">JsonResponse</span>
<span class="token punctuation">{</span>
    <span class="token comment"># 需要指定字段的写法</span>
    <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token class-name static-context">Address</span><span class="token operator">::</span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
        <span class="token string single-quoted-string">'children:id,name,p_id'</span>
    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
    <span class="token comment"># 不需要指定字段</span>
    <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token class-name static-context">Address</span><span class="token operator">::</span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
        <span class="token string single-quoted-string">'children'</span>
    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
    <span class="token comment"># 需要指定条件</span>
    <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token class-name static-context">Address</span><span class="token operator">::</span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
        <span class="token string single-quoted-string">'children'</span><span class="token operator">=&gt;</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token variable">$query</span><span class="token operator">-&gt;</span><span class="token function">slect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
                <span class="token operator">-&gt;</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'status'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
    
<span class="token punctuation">}</span>
    
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h3 id="访问器"><a href="#访问器" class="header-anchor">#</a> 访问器</h3> <blockquote><p>二维和一维都可以用, 避免使用循环查询数据</p></blockquote> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token comment"># Model</span>
<span class="token comment"># 获取器 `not_launch` ,如果model有这个字段, 可以重定义这个字段的值, 如果没有这个字段, 就可给model新增一个字段</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">getNotLaunchAttribute</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">return</span> <span class="token number">100</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment"># 控制器中使用</span>
<span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">userDetail</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">JsonResponse</span>
<span class="token punctuation">{</span>
    <span class="token variable">$created_by</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token property">request</span><span class="token operator">-&gt;</span><span class="token property">id</span><span class="token punctuation">;</span>
    <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token variable">$created_by</span><span class="token punctuation">)</span>       
        <span class="token operator">-&gt;</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment"># 把获取器里面的新增属性`not_launch` 追加到返回值里面去</span>
    <span class="token variable">$data</span><span class="token operator">-&gt;</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'not_launch'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><h3 id="修改器"><a href="#修改器" class="header-anchor">#</a> 修改器</h3> <p>属性已经存在, 直接修改原来的属性值,有点和获取器相反的感觉!</p> <h3 id="with查找出来的对象访问问题"><a href="#with查找出来的对象访问问题" class="header-anchor">#</a> with查找出来的对象访问问题</h3> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token variable">$orderObj</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">with</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
            <span class="token string single-quoted-string">'UserInfo:id,openId'</span>
        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
            <span class="token string single-quoted-string">'id'</span> <span class="token operator">=&gt;</span> <span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
            <span class="token string single-quoted-string">'created_by'</span> <span class="token operator">=&gt;</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token property">request</span><span class="token operator">-&gt;</span><span class="token property">id</span>
        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment"># 访问关联模型UserInfo里面的id,openId</span>
<span class="token variable">$id</span> <span class="token operator">=</span> <span class="token variable">$orderObj</span><span class="token operator">-&gt;</span><span class="token property">UserInfo</span><span class="token operator">-&gt;</span><span class="token property">id</span><span class="token punctuation">;</span>
<span class="token variable">$openId</span> <span class="token operator">=</span> <span class="token variable">$orderObj</span><span class="token operator">-&gt;</span><span class="token property">UserInfo</span><span class="token operator">-&gt;</span><span class="token property">openId</span><span class="token punctuation">;</span>
<span class="token comment"># 修改openId为888</span>
<span class="token variable">$orderObj</span><span class="token operator">-&gt;</span><span class="token property">UserInfo</span><span class="token operator">-&gt;</span><span class="token property">openId</span> <span class="token operator">=</span> <span class="token number">888</span><span class="token punctuation">;</span>
<span class="token variable">$orderObj</span><span class="token operator">-&gt;</span><span class="token property">UserInfo</span><span class="token operator">-&gt;</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="join关联查询"><a href="#join关联查询" class="header-anchor">#</a> <code>join</code>关联查询</h3> <p><code>laravel orm</code>基本都基于子查询, 而子查询在使用中会建立一张临时表进行数据排序等,而join则不会,所以子查询又慢又占用内</p> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token variable">$issues</span> <span class="token operator">=</span> <span class="token class-name static-context">Issue</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'issues.status'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'projects'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$join</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
        <span class="token variable">$join</span><span class="token operator">-&gt;</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'issues.project_id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'projects.id'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">whereIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'projects.id'</span><span class="token punctuation">,</span> <span class="token class-name static-context">ProjectsUser</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">,</span><span class="token variable">$user</span><span class="token operator">-&gt;</span><span class="token property">id</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'project_id'</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token operator">-&gt;</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

 <span class="token variable">$student</span> <span class="token operator">=</span> <span class="token class-name static-context">Student</span><span class="token operator">::</span><span class="token function">leftJoin</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'banji_students as s'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'students.id'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'='</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'s.student_id'</span><span class="token punctuation">)</span>
                <span class="token operator">-&gt;</span><span class="token function">select</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'students.*'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'s.banji_id'</span><span class="token punctuation">)</span>
                <span class="token operator">-&gt;</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'banji_students.banji_id'</span><span class="token punctuation">,</span><span class="token constant">null</span><span class="token punctuation">)</span>
                <span class="token operator">-&gt;</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="orm关联查询代码"><a href="#orm关联查询代码" class="header-anchor">#</a> <code>orm</code>关联查询代码</h3> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token comment">#查询关联模型OrderAppoint中id=4的数据</span>
<span class="token variable">$orderObj</span> <span class="token operator">=</span> <span class="token class-name static-context">Order</span><span class="token operator">::</span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'OrderAppoint'</span><span class="token punctuation">)</span>
            <span class="token operator">-&gt;</span><span class="token function">whereHas</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'OrderAppoint'</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
                <span class="token variable">$query</span><span class="token operator">-&gt;</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span>
            <span class="token operator">-&gt;</span><span class="token function">whereIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'type'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'OrderAppoint'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'OrderTime'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
            <span class="token operator">-&gt;</span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'DESC'</span><span class="token punctuation">)</span>
            <span class="token operator">-&gt;</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

​<span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span><span class="token string backtick-quoted-string">``</span>`
<span class="token comment"># 按昵称或者电话搜索, 没传默认搜索全部</span>
<span class="token variable">$input</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token property">request</span><span class="token operator">-&gt;</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$sou</span> <span class="token operator">=</span> <span class="token keyword">empty</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'sou'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant boolean">true</span> <span class="token operator">?</span> <span class="token constant boolean">false</span> <span class="token punctuation">:</span> <span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'sou'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token variable">$obj</span> <span class="token operator">=</span> <span class="token class-name static-context">UserIdentity</span><span class="token operator">::</span><span class="token function">with</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'UserInfo:id,nickName,avatarUrl,phone,memberLevel,authStatus,sex'</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">whereHas</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'UserInfo'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token keyword">use</span> <span class="token punctuation">(</span><span class="token variable">$sou</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token variable">$query</span><span class="token operator">-&gt;</span><span class="token function">when</span><span class="token punctuation">(</span><span class="token variable">$sou</span><span class="token punctuation">,</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$query</span><span class="token punctuation">,</span><span class="token variable">$sou</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token variable">$query</span><span class="token operator">-&gt;</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'nickName'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'%'</span><span class="token operator">.</span><span class="token variable">$sou</span><span class="token operator">.</span><span class="token string single-quoted-string">'%'</span><span class="token punctuation">)</span>
                <span class="token operator">-&gt;</span><span class="token function">orwhere</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'phone'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'like'</span><span class="token punctuation">,</span><span class="token string single-quoted-string">'%'</span><span class="token operator">.</span><span class="token variable">$sou</span><span class="token operator">.</span><span class="token string single-quoted-string">'%'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">orderBy</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'DESC'</span><span class="token punctuation">)</span>
    <span class="token operator">-&gt;</span><span class="token function">paginate</span><span class="token punctuation">(</span><span class="token keyword">empty</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'size'</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token constant boolean">true</span> <span class="token operator">?</span> <span class="token number">10</span> <span class="token punctuation">:</span> <span class="token variable">$input</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'size'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-&gt;</span><span class="token function">success</span><span class="token punctuation">(</span><span class="token function">page</span><span class="token punctuation">(</span><span class="token variable">$obj</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h3 id="参考with的访问和修改问题"><a href="#参考with的访问和修改问题" class="header-anchor">#</a> 参考with的访问和修改问题</h3> <div class="language-php line-numbers-mode"><pre class="language-php"><code><span class="token variable">$obj</span><span class="token operator">-&gt;</span><span class="token property">name</span><span class="token operator">=</span><span class="token string single-quoted-string">'小明'</span><span class="token punctuation">;</span>
<span class="token variable">$obj</span><span class="token operator">-&gt;</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$obj</span><span class="token operator">-&gt;</span><span class="token property">UserInfo</span><span class="token operator">-&gt;</span><span class="token property">age</span><span class="token operator">=</span><span class="token number">18</span><span class="token punctuation">;</span>
<span class="token comment"># 修改问题</span>
<span class="token variable">$obj</span><span class="token operator">-&gt;</span><span class="token property">UserInfo</span><span class="token operator">-&gt;</span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 可以通过这样, 修改模型和关联模型的数据    </span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h2 id="常用参考资料"><a href="#常用参考资料" class="header-anchor">#</a> 常用参考资料</h2> <ul><li><a href="https://lbs.qq.com/service/webService/webServiceGuide/SmartGeocoder" target="_blank" rel="noopener noreferrer">腾讯地址解析<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated:</span> <span class="time">3/22/2022, 5:25:09 PM</span></div></footer> <!----> </main></div><div class="global-ui"><!----><!----><!----></div></div>
    <script src="/blog/assets/js/app.160d1732.js" defer></script><script src="/blog/assets/js/2.553eb4dc.js" defer></script><script src="/blog/assets/js/23.56de7058.js" defer></script><script src="/blog/assets/js/3.4c6b6694.js" defer></script>
  </body>
</html>
